package editvois

import (
	"fmt"
	"html/template"
	"net/http"
	"net/url"
	"os"

	"code.google.com/awwwe-browser/data"
)

type NavigationCSSData struct {
	MaxWidth                                                                                                                       template.CSS
	NavHeaderSize, AuthNavHeaderWidth, RegistryNavHeaderWidth, ExamplesNavHeaderWidth, NewsNavHeaderWidth, VoisHowToNavHeaderWidth template.CSS
	NavAWidth, NavAHeight, NavASize                                                                                                template.CSS
}

// returns the view data.
func newNavigationCSSData(screenwidth int) *NavigationCSSData {
	var v NavigationCSSData
	if screenwidth > 900 {
		v.MaxWidth = template.CSS("880px")
		v.NavHeaderSize = template.CSS("30px")
		v.AuthNavHeaderWidth = template.CSS("186px")
		v.RegistryNavHeaderWidth = template.CSS("370px")
		v.ExamplesNavHeaderWidth = template.CSS("594px")
		v.NewsNavHeaderWidth = template.CSS("118px")
		v.VoisHowToNavHeaderWidth = template.CSS("614px")
		v.NavAWidth = template.CSS("214px;")
		v.NavAHeight = template.CSS("160px")
		v.NavASize = template.CSS("18px")
	} else {
		v.MaxWidth = template.CSS("600px")
		v.NavHeaderSize = template.CSS("20px")
		v.AuthNavHeaderWidth = template.CSS("128px")
		v.RegistryNavHeaderWidth = template.CSS("254px")
		v.ExamplesNavHeaderWidth = template.CSS("404px")
		v.NewsNavHeaderWidth = template.CSS("82px")
		v.VoisHowToNavHeaderWidth = template.CSS("418px")
		v.NavAWidth = template.CSS("146px;")
		v.NavAHeight = template.CSS("102px")
		v.NavASize = template.CSS("12px")
	}
	return &v
}

// handles the navigation css files.
func HandlerNavigationCSS(w http.ResponseWriter, r *http.Request) {
	var ok bool
	var values url.Values
	var cssdata *NavigationCSSData
	var screenWidth int
	var width []string
	var header http.Header
	header = w.Header()
	header.Add("Content-Type", "text/css")
	values = r.URL.Query()
	width, ok = values["screenWidth"]
	if ok && len(width) > 0 {
		screenWidth, ok = data.StringToInt(width[0])
		if !ok {
			screenWidth = 1024
		}
	} else {
		screenWidth = 1024
	}
	cssdata = newNavigationCSSData(screenWidth)
	var err error
	var t *template.Template
	var cwd, msg string
	// page template
	t, err = template.ParseFiles(fmt.Sprintf("%s/navigationCSS.tmpl", Package))
	if err == nil {
		err = t.Execute(w, cssdata)
	}
	if err != nil {
		msg = err.Error()
		cwd, err = os.Getwd()
		errorOutString(w, fmt.Sprintf("%s<br/>%s", msg, cwd))
	}
}
